<!-- 病毒隔离 -->
<template>
  <div class="dashboard-editor-container">
    <el-row style="background: #fff; padding: 16px 16px 0;">
      <connsearch ref="connsearch" :formOptions="formOptions" @search="search" @reset="reset"/>
      <div class="tabclick">
        <!-- <div class="tabitem" @click="handleClick1()">
          <span>显示日统计趋势<i :class="tab1 ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i></span>
        </div> -->
        <div class="tabitem" @click="handleClick()">
          <span><i :class="tab ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i></span><!-- 显示综合统计 -->
        </div>
      </div>
      <el-row>
        <el-tabs type="border-card" v-if="tab" style="margin-top:16px;" v-model="activeName" @tab-click="tabClick">
          <el-tab-pane label="显示日统计趋势" lazy name="day">
            <el-col :xs="24" :sm="24" :lg="24">
              <barchart :chartData="barchartData" v-if="this.activeName === 'day'" @barclick="barclick"/>
            </el-col>
          </el-tab-pane>
          <el-tab-pane label="按操作用户统计分析" lazy name="user">
            <el-row :gutter="20">
              <el-col :xs="24" :sm="24" :lg="24">
                <piechart  :chartData="pie1Data" v-if="this.activeName === 'user'" :title="'按操作用户统计分析'" @pieclick="pieclick"/>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="按操作方法统计分析" lazy name="class">
            <el-col :xs="24" :sm="24" :lg="24">
              <piechart :chartData="pie2Data" v-if="this.activeName === 'class'"  :title="'按操作方法统计分析'" @pieclick="pieclick"/>
            </el-col>
          </el-tab-pane>
        </el-tabs>
      </el-row>
      <el-col :xs="{ span: 24 }" :sm="{ span: 24 }" :md="{ span: 24 }" :lg="{ span: 24 }" :xl="{ span: 24 }" style="padding-right: 8px;margin:16px 0;">
        <!-- <span class="title">告警日志列表</span> -->
        <el-table stripe border :data="tableData" style="width: 100%; margin-top: 15px">
          <el-table-column label="序号" width="80" type="index" align="left"></el-table-column>
          <el-table-column label="发生时间" prop="TransTime" width="160px" align="left"></el-table-column>
          <el-table-column label="操作工具" prop="EmployeeName" width="160px"  align="left"></el-table-column>
          <el-table-column label="终端用户" prop="Name" align="left"></el-table-column>
          <el-table-column label="文件名称" prop="FileName" align="left"></el-table-column>
          <el-table-column label="病毒描述" prop="VirusDescp" align="left"></el-table-column>
          <el-table-column label="操作" align="left" width="220px">
            <template slot-scope="scope">
              <el-button size="mini" @click="changeMailGlobal(scope.row)">下载</el-button>
              <el-button size="mini" @click="changeMailGlobal(scope.row)">删除</el-button>
              <el-button size="mini" @click="changeMailGlobal(scope.row)">恢复</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination 
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange" 
          :current-page=pageNum
          :page-size=pageSize
          background 
          layout="total, prev, pager, sizes, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import connsearch from "./components/connsearch.vue";
import barchart from "./components/barchart.vue";
import piechart from "./components/piechart.vue";
import { getNowDay, parseTime } from "@/utils/index.js";
import { connrequest } from "@/api/firstPage.js"
export default {
  components: { connsearch, barchart, piechart },
  data() {
    let that = this
    return {
      aaa:'',
      activeName: "day",
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tab: false,
      tab2: false,
      barchartData: {xAxis:[],series:[]},
      pie1Data: [],
      pie2Data: [],
      tableData:[],
      searchform:{},
      formOptions: [
        {
          col: 8,
          hidden: false,
          label: "日期", // label文字
          prop: "timeRange", // 字段名
          initValue: [], // 字段初始值,
          element: "el-date-picker", // 指定elementui组件
          type: "", // 日期选择类型,
        },
        // {
        //   col: 4,
        //   hidden: false,
        //   label: "监测结果", // label文字
        //   prop: "SumCount", // 字段名
        //   element: "el-select", // 指定elementui组件
        //   initValue: 10, // 字段初始值,
        //   placeholder: "请选择监测结果", // elementui组件属性
        //   options: [
        //     { label: '全部', value: 10 },
        //     { label: '严重', value: 20 },
        //     { label: '不严重', value: 30 }
        //   ],
        //   rules: [], // elementui组件属性
        //   events: {
        //     // elementui组件方法
        //     input(val) {
        //       // console.log(val)
        //     },
        //   },
        // },
        {
          col: 4,
          hidden: false,
          label: "操作方法", // label文字
          prop: "Class", // 字段名
          element: "el-select", // 指定elementui组件
          initValue: 0, // 字段初始值,
          placeholder: "请选择操作方法", // elementui组件属性
          name:'FileOptClass',
          options: [],
          rules: [], // elementui组件属性
          events: {
            // elementui组件方法
            input(val) {
              // console.log(val)
            },
          },
        },
        {
          col: 4,
          hidden: false,
          label: "操作行为", // label文字
          prop: "Action", // 字段名
          element: "el-select", // 指定elementui组件
          initValue: 0, // 字段初始值,
          placeholder: "请选择操作类型", // elementui组件属性
          options: [],
          name:'FileOptAction',
          rules: [], // elementui组件属性
          events: {
            // elementui组件方法
            input(val) {
              // console.log(val)
            },
          },
        },
        {
          col: 4,
          hidden: false,
          label: "操作对象", // label文字
          prop: "Type", // 字段名
          element: "el-select", // 指定elementui组件
          initValue: 0, // 字段初始值,
          placeholder: "请选择操作类型", // elementui组件属性
          options: [],
          name:'FileOptType',
          rules: [], // elementui组件属性
          events: {
            // elementui组件方法
            input(val) {
              // console.log(val)
            },
          },
        },
        {
          col: 4,
          label: "终端用户", // label文字
          prop: "Name", // 字段名
          element: "el-select", // 指定elementui组件
          initValue: "", // 字段初始值,
          placeholder: "请选择终端用户", // elementui组件属性
          options: [],
          rules: [], // elementui组件属性
          events: {
            // elementui组件方法
            input(val) {
              // console.log(val);
            },
          },
        },
        {
          col: 8,
          label: "文件名称", // label文字
          prop: "Descp", // 字段名
          element: "el-input", // 指定elementui组件
          initValue: "", // 字段初始值,
          placeholder: "请输入文件名称,支持模糊查询。", // elementui组件属性
          rules: [], // elementui组件属性
          events: {
            // elementui组件方法
            input(val) {
              // console.log(val);
            },
          },
        },
      ],
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.pageSize = this.$store.state.user.DefaultPageLength;
    let xTime = new Date().getTime()
    let endTime = new Date(new Date().setHours(0, 0, 0, 0))
    let startTime = endTime - ( this.$store.state.user.DefaultLogDays * 24 * 60 * 60 *1000);
    let arr = [ parseTime(startTime),'']
    this.formOptions.map( item => {
      if(item.label === '日期'){
        item.initValue = arr
      }
    })
    this.$set(this.searchform,'timeRange', arr)
    this.$set(this.searchform,'SumCount', 10)
    this.getData()
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.GetEmployee()
    this.GetVirusFileLogSumGroupByDate()
  },
  methods: {
    // 获取所有数据
    getData(){
      this.GetVirusFileLog()
      this.GetUIDictParam('')
      // this.GetVirusFileLogSumGroupByUser()
      // this.GetVirusFileLogSumGroupByClass()
      // this.GetVirusFileLogSumGroupByDate()
    },
    tabClick(tab){
      this.reset()
      if(tab.name === 'day'){
        this.GetVirusFileLogSumGroupByDate()
      }else if(tab.name === 'user'){
        this.GetVirusFileLogSumGroupByUser();
      }else if(tab.name === 'class'){
        this.GetVirusFileLogSumGroupByClass()
      }
    },
    barclick(val){
      this.pageNum = 1
      this.searchform.timeRange = [ `${val} 00:00:00`, `${val} 23:59:59` ]
      // let arr = [ `${val} 00:00:00`, `${val} 23:59:59` ]
      // this.formOptions = this.formOptions.map( item => {
      //   if(item.label === "日期"){
      //     item.initValue = arr
      //   }else if(item.label === "统计排名"){
      //     item.initValue = 10
      //   }else{
      //     item.initValue = ""
      //   }
      //   return item
      // })
      // this.$set(this.searchform, 'timeRange', arr)
      // this.$refs.connsearch.addInitValue()
      this.GetVirusFileLog()
    },
    pieclick(val,title,id){
      this.pageNum = 1
      if(title === '操作方法统计分析'){
        this.searchform.Class = id
        this.$refs.connsearch.searchform.Class = id
      }else if (title ==='操作用户统计分析'){
        this.searchform.Name = val
        this.$refs.connsearch.searchform.Name = val
      }
      // if(title === '访问用户统计分析'){
      //   this.$set(this.searchform, 'Name', val)
      //   this.formOptions = this.formOptions.map( item => {
      //     if(item.label === "日期"){
      //       item.initValue = this.searchform.timeRange
      //     }else if(item.label === "统计排名"){
      //       item.initValue = 10
      //     }else if(item.label === "终端用户"){
      //       item.initValue = val
      //     }else{
      //       item.initValue = ""
      //     }
      //     return item
      //   })
      // }else{
      //   this.$set(this.searchform, 'Descp', val)
      //   this.$set(this.searchform, 'timeRange', this.searchform.timeRange)
      //   this.formOptions = this.formOptions.map( item => {
      //     if(item.label === "日期"){
      //       item.initValue = this.searchform.timeRange
      //     }else if(item.label === "统计排名"){
      //       item.initValue = 10
      //     }else if(item.label === "描述信息"){
      //       item.initValue = val
      //     }else{
      //       item.initValue = ""
      //     }
      //     return item
      //   })
      // }
      // this.$refs.connsearch.addInitValue()
      this.GetVirusFileLog()
    },
    async GetUIDictParam(type){
      const data = {
        Data : {
          FilterType:type
        }
      }
      const res = await connrequest('GetUIDictParam',data)
      // this.Dictionaries = res.Data;
      console.log(res);
      let arr = [{value: 0, label: '全部'}]
      if (type === '') {
        this.formOptions.map( item => {
          item.options = [{value: 0, label: '全部'}]
          for (const key in res.Data) {
            if(item.name === key){
              res.Data[key].map(items=>{
                item.options.push({
                  label:items.Descp,
                  value:items.Value
                })
              })
            }
          }
        })
        
      } else {
        
      }
     
  },
    // 获取终端用户
    async GetEmployee(){
      const res = await connrequest('GetEmployee')
      res.Data = res.Data.map(item => {
        item.label = item.Department +' / '+item.Name 
        item.value = item.Name
        return item
      })
      let arr = [{value: '', label: '全部'}]
      this.formOptions.map( item => {
        if(item.label === '终端用户'){
          item.options = [...arr,...res.Data]
        }
      })
    },
    // 获取事件日志列表
    async GetVirusFileLog(){
      const data = {
        Paging: {
          QueryPageNo: this.pageNum,
          QueryPageLength: this.pageSize
        },
        Filter: {
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          VirusStatus:this.searchform.VirusStatus,
          Name: this.searchform.Name || '',
          Descp: this.searchform.Descp || '',
          Action: this.searchform.Action ,
          Class: this.searchform.Class,
          Type: this.searchform.Type,
          SumCount: 10
        }
      }
      const res = await connrequest('GetVirusFileLog', data)
      res.Data = res.Data.map(item => {
        item.TransTime = parseTime(item.TransTime)
        return item
      })
      this.tableData = res.Data
      this.total = res.Paging.TotalCount
    },
    // 获取事件用户统计
    async GetVirusFileLogSumGroupByUser(){
      let arr = []
      const data = {
        Filter:{
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          VirusStatus:this.searchform.VirusStatus,
          Name: this.searchform.Name || '',
          Descp: this.searchform.Descp || '',
          Action: this.searchform.Action ,
          Class: this.searchform.Class,
          Type: this.searchform.Type,
          SumCount: 10
        }
      }
      const res = await connrequest('GetVirusFileLogSumGroupByUser', data)
      res.Data.map( item =>{
        arr.push({name: item.Name, value: item.Count})
      })
      this.pie1Data = arr
    },
    // 按照操作方法统计
    async GetVirusFileLogSumGroupByClass(){
      let arr = []
      const data = {
        Filter:{
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          VirusStatus:this.searchform.VirusStatus,
          Name: this.searchform.Name || '',
          Descp: this.searchform.Descp || '',
          Action: this.searchform.Action ,
          Class: this.searchform.Class,
          Type: this.searchform.Type,
          SumCount: 10
        }
      }
      const res = await connrequest('GetVirusFileLogSumGroupByClass', data)
      if(res.Status.StatusCode === 0){
        res.Data.map( item =>{
          this.formOptions.map( formItem => {
            if(formItem.label === '操作方法'){
              formItem.options.map( newitem =>{
                  if(item.ClassID === newitem.value){
                    // console.log(111111);
                    item.Name = newitem.label
                  }
              })
            }
          })
          arr.push({name: item.Name, id: item.ClassID, value: item.Count})
        })
      }
      this.pie2Data = arr
    },
    // 获取事件日期统计
    async GetVirusFileLogSumGroupByDate(){
      let xAxis = []
      let series= []
      const data = {
        Filter:{
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          VirusStatus:this.searchform.VirusStatus,
          Name: this.searchform.Name || '',
          Descp: this.searchform.Descp || '',
          Action: this.searchform.Action ,
          Class: this.searchform.Class,
          Type: this.searchform.Type,
          SumCount: 10
        }
      }
      const res = await connrequest('GetVirusFileLogSumGroupByDate', data)
      res.Data.map( item =>{
        xAxis.push(item.Name)
        series.push(item.Count)
      })
      this.barchartData = {xAxis,series}
    },
    // 搜索
    search(val) {
      this.pageNum = 1
      this.searchform = {...val}
      this.getData()
      if(this.activeName === 'day'){
        this.GetVirusFileLogSumGroupByDate()
      }else if(this.activeName === 'user'){
        this.GetVirusFileLogSumGroupByUser();
      }else if(this.activeName === 'class'){
        this.GetVirusFileLogSumGroupByClass()
      }
    },
    // 重置
    reset(){
      let xTime = new Date().getTime()
      let endTime = new Date(new Date().setHours(0, 0, 0, 0))
      let startTime = endTime - (window.sessionStorage.getItem('DefaultLogDays') * 1 * 3600 * 24 * 1000);
      let arr = [ parseTime(startTime),'']
      this.pageNum = 1
      this.formOptions = this.formOptions.map( item => {
        if(item.label === "日期"){
          item.initValue = arr
        }else if(item.label === "统计排名"){
          item.initValue = 10
        }else{
          item.initValue = ""
        }
        return item
      })
      this.searchform = {}
      this.$set(this.searchform,'timeRange', arr)
      this.$set(this.searchform,'SumCount', 10)
      this.$refs.connsearch.addInitValue()
      this.getData()
    },
    handleSizeChange(pageSize){
      this.pageSize = pageSize
      this.pageNum = 1
      this.GetVirusFileLog()
    },
    handleCurrentChange( pageNum ){
      this.pageNum = pageNum
      this.GetVirusFileLog()
    },
    handleClick() {
      this.tab = !this.tab
    },
    handleClick2() {
      this.tab2 = !this.tab2
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./components/css/conncss.scss";
</style>